package com.awesome.videodemo.video.media;

import android.media.MediaPlayer;
import android.view.SurfaceHolder;

import com.awesome.common.util.log.ShowLogUtil;
import com.awesome.videodemo.util.TimeFormatUtil;
import com.awesome.videodemo.video.AbsPlayer;
import com.awesome.videodemo.video.exo.IMediaPlayerListener;

import java.io.IOException;

/**
 * Author: zhangjingfang
 * Email:zhangjingfang@jeejio.com
 * Date: 2022/7/21
 * Description:类描述
 */
public class NativePlayer extends AbsPlayer {
    private MediaPlayer mMediaPlayer;

    public NativePlayer(String path) {
        mMediaPlayer = new MediaPlayer();
        try {
            mMediaPlayer.setDataSource(path);
            mMediaPlayer.prepareAsync();
            mMediaPlayer.setOnPreparedListener(new android.media.MediaPlayer.OnPreparedListener() {
                @Override
                public void onPrepared(android.media.MediaPlayer mp) {
                    ShowLogUtil.info(TAG,"onPrepared");
                    if (mMediaPlayerListener != null)
                        mMediaPlayerListener.onPrepared();
                    start();
                    pause();
                }
            });
            mMediaPlayer.setOnCompletionListener(new android.media.MediaPlayer.OnCompletionListener() {
                @Override
                public void onCompletion(android.media.MediaPlayer mp) {
                    ShowLogUtil.info(TAG,"onCompletion");
                    if (mMediaPlayerListener != null)
                        mMediaPlayerListener.onComplete();
                    pause();
                }
            });
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void start() {
        mMediaPlayer.start();
    }

    @Override

    public void pause() {
        mMediaPlayer.pause();
    }

    public void seekTo(long msec) {
        mMediaPlayer.seekTo((int) msec);
    }

    public void setDisplay(SurfaceHolder sh) {
        mMediaPlayer.setDisplay(sh);
    }

    public void setOnVideoSizeChangedListener(MediaPlayer.OnVideoSizeChangedListener listener) {
        mMediaPlayer.setOnVideoSizeChangedListener(listener);
    }

    @Override
    public boolean isPlaying() {
       return mMediaPlayer.isPlaying();
    }

    @Override
    public long getDuration() {
        return mMediaPlayer.getDuration();
    }
    @Override
    public long getCurrentPosition() {
        return mMediaPlayer.getCurrentPosition();
    }

    public MediaPlayer getMediaPlayer() {
        return mMediaPlayer;
    }

}
